import { ref } from 'vue'; 
import { useToolBar as ToolBarList } from "@/store";
import type * as Type from '@/store/modules/toolBar/type';

export default function useToolBar(provideState: { getToolBarItem: Function }) {

  const toolBarList = ToolBarList();
  
  // 工具展示状态
  const visibile = ref(true);
  //画笔半径
  const brushRadius = ref(10);
  //画笔层数
  const brushLayer = ref(1);
  //橡皮擦半径
  const eraserRadius = ref(10);
  //橡皮擦层数
  const eraserLayer = ref(1);
  //长方体边长
  const sideLength = ref(20);


  // 工具标题点击
  const handleTitleClick = (flag: boolean) => {
    visibile.value = !flag;
  };

  // 工具列表点击
  const handleListClick = (item: Type.MarkDataList) => {
    toolBarList.markToolList.map((item: { isVisible: boolean; }) => item.isVisible = false);
    item.isVisible = !item.isVisible;
    toolBarList.setCurrentId(0, true);
    if (item.label === '橡皮擦') {
      provideState.getToolBarItem(() => {
        return {
          currentItem: toolBarList.setCurrentId(item.id, true),
          toolType: item.toolType,
          radius: eraserRadius.value,
          layers: eraserLayer.value
        };
      });
    }/*else if(item.toolType === 'CUBOID'){
      provideState.getToolBarItem(() => {
        return {
          currentItem: toolBarList.setCurrentId(item.id, true),
          toolType: item.toolType,
          sideLength: sideLength.value
        };
      });
    }*/
  };

  // 工具栏下病灶选中
  const handleLesionClick = (item: Type.MarkDataList, it: Type.NidusItem) => {
    item.isVisible = !item.isVisible;
    provideState.getToolBarItem(() => {
      return {
        currentItem: toolBarList.setCurrentId(item.id, true),
        lesion: it
      };
    });
  };

  // 工具栏选择层数和半径、边长
  const handleSliderChange = (item: Type.MarkDataList, type: string = '', value: number | [number, number]) => {
    const radius = (type === 'brushRadius'? brushRadius.value : eraserRadius.value)
    const layers = (type === 'brushLayer'? brushLayer.value : eraserLayer.value)
    provideState.getToolBarItem(() => {
      return {
        currentItem: toolBarList.setCurrentId(item.id, true),
        toolType: type,
        value,
        radius,
        layers
      };
    });
  };

  // 自由画笔病灶选择
  const handleBrushLesionClick = (item: Type.MarkDataList, it: Type.NidusItem) => {
    item.isSecondVisible = !item.isSecondVisible;
    item.lesionValue = it.name;
    provideState.getToolBarItem(() => {
      return {
        currentItem: toolBarList.setCurrentId(item.id, true),
        lesion: it,
        radius: brushRadius.value,
        layers: brushLayer.value
      };
    });
  }

  return {
    visibile,
    brushRadius,
    brushLayer,
    eraserRadius,
    eraserLayer,
    sideLength,
    handleTitleClick,
    handleListClick,
    handleLesionClick,
    handleSliderChange,
    handleBrushLesionClick
  };
}